"""
Case Type   : 资源池化集群DMS reform优化
Case Name   : 备机加入
Create At   : 2024/05/13
Owner       : @kyleze
Description :
    1.踢出备机1
    2.加入备机1
    3.查看集群状态
Expect      :
    1.成功
    2.成功
    3.集群状态正常
History     :
"""

import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node


class DmsRform(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.dirname(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH("PrimaryDbUser")

    def test_dms(self):
        step = '--step1:踢出备机1 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl("stop", '-n 2')
        self.log.info(result)
        self.assertIn(self.constant.cm_stop_node_success_msg, "执行失败" + step)

        step = '--step2:加入备机1 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl("start", '-n 2')
        self.log.info(result)
        self.assertIn(self.constant.cm_start_node_success_msg, "执行失败" + step)

        step = '--step3:查看集群状态 expect:集群状态正常--'
        self.log.info(step)
        time.sleep(5)
        status = self.primary_sh.get_db_cluster_status('status')
        self.assertTrue("Normal" in status)

    def tearDown(self):
        self.log.info(f'----{os.path.dirname(__file__)} end----')

